Telegram Group & Telegram Channel
🧠 C# Π—Π°Π΄Π°Ρ‡Π°: β€œΠ›ΠΎΠ²ΡƒΡˆΠΊΠ° замыкания Π² циклС”

πŸ“œ УсловиС:

ΠŸΠΎΡΠΌΠΎΡ‚Ρ€ΠΈ Π½Π° этот ΠΊΠΎΠ΄ ΠΈ скаТи, Ρ‡Ρ‚ΠΎ ΠΎΠ½ Π²Ρ‹Π²Π΅Π΄Π΅Ρ‚:


using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
static void Main()
{
var actions = new List<Action>();

for (int i = 0; i < 5; i++)
{
actions.Add(() => Console.Write(i + " "));
}

foreach (var action in actions)
{
action();
}
}
}


❓ Вопросы:
1. Π§Ρ‚ΠΎ Π½Π°ΠΏΠ΅Ρ‡Π°Ρ‚Π°Π΅Ρ‚ ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ°?
2. ΠŸΠΎΡ‡Π΅ΠΌΡƒ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ ΠΌΠΎΠΆΠ΅Ρ‚ ΡƒΠ΄ΠΈΠ²ΠΈΡ‚ΡŒ?
3. Как ΠΈΡΠΏΡ€Π°Π²ΠΈΡ‚ΡŒ ΠΊΠΎΠ΄, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ 0 1 2 3 4?

⚠️ ΠŸΠΎΠ΄Π²ΠΎΡ…:

- ΠŸΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Π°Ρ i захватываСтся ΠΏΠΎ ссылкС всСми лямбдами Π² List<Action>, Π° Π½Π΅ копируСтся Π² ΠΌΠΎΠΌΠ΅Π½Ρ‚ добавлСния.
- Когда Ρ†ΠΈΠΊΠ» Π·Π°Π²Π΅Ρ€ΡˆΠΈΡ‚ΡΡ, i == 5, ΠΈ всС лямбды ΠΎΠ±Ρ€Π°Ρ‰Π°ΡŽΡ‚ΡΡ ΠΊ ΠΎΠ΄Π½ΠΎΠΌΡƒ ΠΈ Ρ‚ΠΎΠΌΡƒ ΠΆΠ΅ `i`, ΡƒΠΆΠ΅ Ρ€Π°Π²Π½ΠΎΠΌΡƒ 5.

---

### βœ… ΠžΠΆΠΈΠ΄Π°Π΅ΠΌΡ‹ΠΉ Π²Ρ‹Π²ΠΎΠ΄:


5 5 5 5 5


πŸ› οΈ Π˜ΡΠΏΡ€Π°Π²Π»Π΅Π½ΠΈΠ΅:

Π§Ρ‚ΠΎΠ±Ρ‹ каТдая лямбда Π·Π°Ρ…Π²Π°Ρ‚Ρ‹Π²Π°Π»Π° свою копию `i`, Π½ΡƒΠΆΠ½ΠΎ Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ ΠΏΡ€ΠΎΠΌΠ΅ΠΆΡƒΡ‚ΠΎΡ‡Π½ΡƒΡŽ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΡƒΡŽ:


for (int i = 0; i < 5; i++)
{
int copy = i;
actions.Add(() => Console.Write(copy + " "));
}


Π’Π΅ΠΏΠ΅Ρ€ΡŒ Π²Ρ‹Π²ΠΎΠ΄ Π±ΡƒΠ΄Π΅Ρ‚:


0 1 2 3 4


🎯 Π§Ρ‚ΠΎ провСряСт Π·Π°Π΄Π°Ρ‡Π°:

- Π—Π½Π°Π½ΠΈΠ΅ ΠΌΠ΅Ρ…Π°Π½ΠΈΠΊΠΈ Π·Π°ΠΌΡ‹ΠΊΠ°Π½ΠΈΠΉ Π² C#
- ПониманиС Ρ€Π°Π·Π½ΠΈΡ†Ρ‹ ΠΌΠ΅ΠΆΠ΄Ρƒ значСниями ΠΈ ссылками Π² Π·Π°ΠΌΡ‹ΠΊΠ°Π½ΠΈΠΈ
- Π£ΠΌΠ΅Π½ΠΈΠ΅ ΠΎΡ‚Π»Π°ΠΆΠΈΠ²Π°Ρ‚ΡŒ Π½Π΅ΠΎΠΆΠΈΠ΄Π°Π½Π½Ρ‹Π΅ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Ρ‹ Π² LINQ ΠΈ Π΄Π΅Π»Π΅Π³Π°Ρ‚Π°Ρ…



tg-me.com/csharp_1001_notes/687
Create:
Last Update:

🧠 C# Π—Π°Π΄Π°Ρ‡Π°: β€œΠ›ΠΎΠ²ΡƒΡˆΠΊΠ° замыкания Π² циклС”

πŸ“œ УсловиС:

ΠŸΠΎΡΠΌΠΎΡ‚Ρ€ΠΈ Π½Π° этот ΠΊΠΎΠ΄ ΠΈ скаТи, Ρ‡Ρ‚ΠΎ ΠΎΠ½ Π²Ρ‹Π²Π΅Π΄Π΅Ρ‚:


using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
static void Main()
{
var actions = new List<Action>();

for (int i = 0; i < 5; i++)
{
actions.Add(() => Console.Write(i + " "));
}

foreach (var action in actions)
{
action();
}
}
}


❓ Вопросы:
1. Π§Ρ‚ΠΎ Π½Π°ΠΏΠ΅Ρ‡Π°Ρ‚Π°Π΅Ρ‚ ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ°?
2. ΠŸΠΎΡ‡Π΅ΠΌΡƒ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ ΠΌΠΎΠΆΠ΅Ρ‚ ΡƒΠ΄ΠΈΠ²ΠΈΡ‚ΡŒ?
3. Как ΠΈΡΠΏΡ€Π°Π²ΠΈΡ‚ΡŒ ΠΊΠΎΠ΄, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ 0 1 2 3 4?

⚠️ ΠŸΠΎΠ΄Π²ΠΎΡ…:

- ΠŸΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Π°Ρ i захватываСтся ΠΏΠΎ ссылкС всСми лямбдами Π² List<Action>, Π° Π½Π΅ копируСтся Π² ΠΌΠΎΠΌΠ΅Π½Ρ‚ добавлСния.
- Когда Ρ†ΠΈΠΊΠ» Π·Π°Π²Π΅Ρ€ΡˆΠΈΡ‚ΡΡ, i == 5, ΠΈ всС лямбды ΠΎΠ±Ρ€Π°Ρ‰Π°ΡŽΡ‚ΡΡ ΠΊ ΠΎΠ΄Π½ΠΎΠΌΡƒ ΠΈ Ρ‚ΠΎΠΌΡƒ ΠΆΠ΅ `i`, ΡƒΠΆΠ΅ Ρ€Π°Π²Π½ΠΎΠΌΡƒ 5.

---

### βœ… ΠžΠΆΠΈΠ΄Π°Π΅ΠΌΡ‹ΠΉ Π²Ρ‹Π²ΠΎΠ΄:


5 5 5 5 5


πŸ› οΈ Π˜ΡΠΏΡ€Π°Π²Π»Π΅Π½ΠΈΠ΅:

Π§Ρ‚ΠΎΠ±Ρ‹ каТдая лямбда Π·Π°Ρ…Π²Π°Ρ‚Ρ‹Π²Π°Π»Π° свою копию `i`, Π½ΡƒΠΆΠ½ΠΎ Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ ΠΏΡ€ΠΎΠΌΠ΅ΠΆΡƒΡ‚ΠΎΡ‡Π½ΡƒΡŽ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΡƒΡŽ:


for (int i = 0; i < 5; i++)
{
int copy = i;
actions.Add(() => Console.Write(copy + " "));
}


Π’Π΅ΠΏΠ΅Ρ€ΡŒ Π²Ρ‹Π²ΠΎΠ΄ Π±ΡƒΠ΄Π΅Ρ‚:


0 1 2 3 4


🎯 Π§Ρ‚ΠΎ провСряСт Π·Π°Π΄Π°Ρ‡Π°:

- Π—Π½Π°Π½ΠΈΠ΅ ΠΌΠ΅Ρ…Π°Π½ΠΈΠΊΠΈ Π·Π°ΠΌΡ‹ΠΊΠ°Π½ΠΈΠΉ Π² C#
- ПониманиС Ρ€Π°Π·Π½ΠΈΡ†Ρ‹ ΠΌΠ΅ΠΆΠ΄Ρƒ значСниями ΠΈ ссылками Π² Π·Π°ΠΌΡ‹ΠΊΠ°Π½ΠΈΠΈ
- Π£ΠΌΠ΅Π½ΠΈΠ΅ ΠΎΡ‚Π»Π°ΠΆΠΈΠ²Π°Ρ‚ΡŒ Π½Π΅ΠΎΠΆΠΈΠ΄Π°Π½Π½Ρ‹Π΅ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Ρ‹ Π² LINQ ΠΈ Π΄Π΅Π»Π΅Π³Π°Ρ‚Π°Ρ…

BY C# 1001 notes


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/csharp_1001_notes/687

View MORE
Open in Telegram


C 1001 notes Telegram | DID YOU KNOW?

Date: |

Should I buy bitcoin?

β€œTo the extent it is used I fear it’s often for illicit finance. It’s an extremely inefficient way of conducting transactions, and the amount of energy that’s consumed in processing those transactions is staggering,” the former Fed chairwoman said. Yellen’s comments have been cited as a reason for bitcoin’s recent losses. However, Yellen’s assessment of bitcoin as a inefficient medium of exchange is an important point and one that has already been raised in the past by bitcoin bulls. Using a volatile asset in exchange for goods and services makes little sense if the asset can tumble 10% in a day, or surge 80% over the course of a two months as bitcoin has done in 2021, critics argue. To put a finer point on it, over the past 12 months bitcoin has registered 8 corrections, defined as a decline from a recent peak of at least 10% but not more than 20%, and two bear markets, which are defined as falls of 20% or more, according to Dow Jones Market Data.

Telegram and Signal Havens for Right-Wing Extremists

Since the violent storming of Capitol Hill and subsequent ban of former U.S. President Donald Trump from Facebook and Twitter, the removal of Parler from Amazon’s servers, and the de-platforming of incendiary right-wing content, messaging services Telegram and Signal have seen a deluge of new users. In January alone, Telegram reported 90 million new accounts. Its founder, Pavel Durov, described this as β€œthe largest digital migration in human history.” Signal reportedly doubled its user base to 40 million people and became the most downloaded app in 70 countries. The two services rely on encryption to protect the privacy of user communication, which has made them popular with protesters seeking to conceal their identities against repressive governments in places like Belarus, Hong Kong, and Iran. But the same encryption technology has also made them a favored communication tool for criminals and terrorist groups, including al Qaeda and the Islamic State.

C 1001 notes from br


Telegram C# 1001 notes
FROM USA